package com.xyongfeng.foodManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyongfeng.foodManagement.common.ErrorCode;
import com.xyongfeng.foodManagement.domain.*;
import com.xyongfeng.foodManagement.domain.FoodInInfo;
import com.xyongfeng.foodManagement.domain.vo.AdminInfoVO;
import com.xyongfeng.foodManagement.exception.BusinessException;
import com.xyongfeng.foodManagement.exception.ThrowUtils;
import com.xyongfeng.foodManagement.mapper.FoodInInfoMapper;
import com.xyongfeng.foodManagement.service.AdminInfoService;
import com.xyongfeng.foodManagement.service.FoodCategoryInfoService;
import com.xyongfeng.foodManagement.service.FoodInInfoService;
import com.xyongfeng.foodManagement.service.FoodInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xyongfeng
 * @description 针对表【food_in_info】的数据库操作Service实现
 * @createDate 2023-12-17 17:02:37
 */
@Service
public class FoodInInfoServiceImpl extends ServiceImpl<FoodInInfoMapper, FoodInInfo>
        implements FoodInInfoService {
    @Autowired
    private AdminInfoService adminInfoService;
    @Autowired
    private FoodInfoService foodInfoService;

    @Override
    public void insert(FoodInInfo foodInInfo, HttpServletRequest request) {

        AdminInfoVO loginAdmin = adminInfoService.getLoginAdmin(request);
        ThrowUtils.throwIf(loginAdmin == null, new BusinessException(ErrorCode.NOT_LOGIN_ERROR));
        foodInInfo.setInAdminId(loginAdmin.getAdminId());
        boolean save = save(foodInInfo);
        ThrowUtils.throwIf(!save, new BusinessException(ErrorCode.OPERATION_ERROR, "添加失败"));

        // 将食品库存添加
        save = foodInfoService.update(new UpdateWrapper<FoodInfo>().eq("food_id", foodInInfo.getInFoodId())
                .setSql(String.format("`food_stock` = `food_stock` + %d", foodInInfo.getInNum())));

        ThrowUtils.throwIf(!save, new BusinessException(ErrorCode.OPERATION_ERROR, "食品库存更新失败"));

    }

    @Override
    public List<FoodInInfo> selectAll() {
        List<FoodInInfo> list = lambdaQuery().orderByDesc(FoodInInfo::getInTime).list();
        // 绑定管理员与
        list = list.stream().peek((x) -> {
            AdminInfo adminInfo =
                    adminInfoService.getOne(new QueryWrapper<AdminInfo>()
                            .eq("admin_id", x.getInAdminId()));
            FoodInfo foodInfo =
                    foodInfoService.getOne(new QueryWrapper<FoodInfo>()
                            .eq("food_id", x.getInFoodId()));

            x.setAdminInfo(adminInfo);
            x.setFoodInfo(foodInfo);
        }).collect(Collectors.toList());
        return list;
    }
}




